package model

import (
	"fmt"
	"gorm.io/gorm"
	"mytest/database"
	"strings"
)

type User struct {
	gorm.Model
	Name string `json:"name"`
	Age  string `json:"age"`
	Sex  string `json:"sex"`
}

func (u *User) MultInsert(reportForms []User) {
	tx, err := database.SqlDB.Begin()
	sqlStr := "INSERT INTO user (name,age,sex) VALUES "
	vals := make([]interface{}, 10)
	const rowSQL = "(?, ?, ?)"
	var inserts []string
	for _, elem := range reportForms {
		inserts = append(inserts, rowSQL)
		vals = append(vals, elem.ID, elem.Name, elem.Age, elem.Sex)
	}
	sqlStr = sqlStr + strings.Join(inserts, ",")
	_, err = tx.Exec(sqlStr, vals...)
	if err != nil {
		_ = tx.Rollback()
		fmt.Print(err)
	} else {
		_ = tx.Commit()
	}
}
